package com.bidevalution.business.job;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.function.FunctionCalculator;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionParameter;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.util.MapUtil;
import com.qtp.core.util.GsonUtil;
/**
 * 公式计算
 * @author daizhifeng   
 * @date 2019年9月12日 上午9:36:27 
 * @version V2.0
 */
public class ParameterFormulaExecuteJob implements StepJobJdbcFactory{
	private JdbcTemplateService jdbcTemplateService;

	@Override
	public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
		this.jdbcTemplateService = jdbcTemplateService;
	}

	@Override
	public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode,String expertId,String layoutCode)
			throws Exception {
		if(!StringUtils.isEmpty(beSectionStepJob.getJobContent())) {
			String[] params = StringUtils.delimitedListToStringArray(beSectionStepJob.getJobContent(), "|");
			if(params.length<2) {
				return false;
			}
			Map<String, Object> env = new HashMap<String, Object>();
			env.put("$sectionStepId", beSectionStepJob.getSectionStepId());
			env.put("$submitCode", submitCode);
			env.put("$sectionId", beSectionStepJob.getSectionId());
			env.put("$expertId", expertId);
			env.put("$layoutCode", layoutCode);
			for(Map<String, Object> dataMap:jdbcTemplateService.selectForList("select param_id,param_value from be_section_parameter where section_id=:sectionId", MapUtil.create().putValue("sectionId", beSectionStepJob.getSectionId()).getMap())) {
				env.put("$PARAM_"+dataMap.get("param_id"), dataMap.get("param_value"));
			}
			String formularConfig = params[1];
			Object resultObject = null;
			formularConfig = formularConfig.trim();
			if(formularConfig.toLowerCase().startsWith("{ael[")) {
				formularConfig = formularConfig.substring(5, formularConfig.length() - 2);
				resultObject = FunctionCalculator.computeAviator(formularConfig, env);
			}else if(formularConfig.toLowerCase().startsWith("{mel[")) {
				formularConfig = formularConfig.substring(5, formularConfig.length() - 2);
				resultObject = FunctionCalculator.computeMVEL(formularConfig, env);
			}
			System.out.println(resultObject+"resultObjectresultObjectresultObject");
			Map<String,Object> paramMap = MapUtil.create().putValue("sectionId", beSectionStepJob.getSectionId()).putValue("paramValue", resultObject).putValue("paramId", params[0]).getMap();
			//删除已存在结果
			jdbcTemplateService.update(" DELETE FROM be_section_parameter WHERE section_id=:sectionId and param_id=:paramId ", paramMap);

			jdbcTemplateService.update(
					"insert into be_section_parameter(section_id,param_id,param_type,param_value,create_time,update_time) values(:sectionId,:paramId,2,   :paramValue,now(),now())",paramMap);
		}
		return true;
	}

}
